//
//  DoITListViewController.m
//  DoIT
//
//  Created by gex on 14/11/10.
//  Copyright (c) 2014年 gex. All rights reserved.
//

#import "DoITListViewController.h"
#import "AddDoITViewController.h"
#import "Todo.h"
#import "TodoService.h"
#import "DatabaseCreator.h"

@interface DoITListViewController ()
{
    NSMutableArray *todoes;
}
@end

@implementation DoITListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.rowHeight = 50;
    self.tableView.allowsSelection = NO; // We essentially implement our own selection
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [DatabaseCreator initDatabase];
    [self initDate];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initList) name:NOTIF_KEY_REDO object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(void)initDate
{
    todoes = [NSMutableArray array];
    [todoes addObjectsFromArray:[[TodoService sharedTodoService] getAllTodoByStatus:TodoStatusDoing]];
    
}
-(void)initList
{
    [self initDate];
    [self.tableView reloadData];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [todoes count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        NSMutableArray *leftUtilityButtons = [NSMutableArray new];
        NSMutableArray *rightUtilityButtons = [NSMutableArray new];
        
        [leftUtilityButtons addUtilityButtonWithColor:
         [UIColor colorWithRed:0.07 green:0.75f blue:0.16f alpha:1.0]
                                                 icon:[UIImage imageNamed:@"check.png"]];
        //        [leftUtilityButtons addUtilityButtonWithColor:
        //         [UIColor colorWithRed:1.0f green:1.0f blue:0.35f alpha:1.0]
        //                                                 icon:[UIImage imageNamed:@"clock.png"]];
        //        [leftUtilityButtons addUtilityButtonWithColor:
        //         [UIColor colorWithRed:1.0f green:0.231f blue:0.188f alpha:1.0]
        //                                                 icon:[UIImage imageNamed:@"cross.png"]];
        //        [leftUtilityButtons addUtilityButtonWithColor:
        //         [UIColor colorWithRed:0.55f green:0.27f blue:0.07f alpha:1.0]
        //                                                 icon:[UIImage imageNamed:@"list.png"]];
        //
        //        [rightUtilityButtons addUtilityButtonWithColor:
        //         [UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0]
        //                                                 title:@"More"];
        [rightUtilityButtons addUtilityButtonWithColor:
         [UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f]
                                                  icon:[UIImage imageNamed:@"cross.png"]];
        
        cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:cellIdentifier
                                  containingTableView:self.tableView // Used for row height and selection
                                   leftUtilityButtons:leftUtilityButtons
                                  rightUtilityButtons:rightUtilityButtons];
        cell.delegate = self;
    }
    Todo *toDo= [todoes objectAtIndex:indexPath.row];
    cell.textLabel.text = toDo.content;
    NSLog(@"todo:%@",toDo);
    return cell;
}

#pragma mark - SWTableViewDelegate

- (void)swippableTableViewCell:(SWTableViewCell *)cell didTriggerLeftUtilityButtonWithIndex:(NSInteger)index {
    switch (index) {
        case 0:
        {
            // Delete button was pressed
            NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
            Todo *todo = [todoes objectAtIndex:cellIndexPath.row];
            todo.status = [NSNumber numberWithInteger:TodoStatusDone];
            [[TodoService sharedTodoService] modifyTodo:todo];
            [todoes removeObject:todo];
            [self.tableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
            break;
            
        }
        case 1:
            NSLog(@"left button 1 was pressed");
            break;
        default:
            break;
    }
}

- (void)swippableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index {
    switch (index) {
        case 0:
        {
            NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
            [[TodoService sharedTodoService] removeTodo:[todoes objectAtIndex:cellIndexPath.row]];
            [todoes removeObjectAtIndex:cellIndexPath.row];
            [self.tableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
            break;
        }
        case 1:
        {
            NSLog(@"More button was pressed");
            UIAlertView *alertTest = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"More more more" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles: nil];
            [alertTest show];
            
            [cell hideUtilityButtonsAnimated:YES];
            break;
        }
        default:
            break;
    }
}
#pragma -back on back event
-(IBAction)onBackDoITList:(UIStoryboardSegue *)segue{
    AddDoITViewController *addDoITViewController = [segue sourceViewController];
    Todo *todo=addDoITViewController.todo;
    if(todo){
        [todoes insertObject:todo atIndex:0];
        [self.tableView reloadData];
        [[TodoService sharedTodoService] addTodo:todo];
    }
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
